'use strict';

const Service = require('egg').Service;
const moment = require('moment'); 

class UserService extends Service {
    async registry(username,password,num) {
        //userlist ---> uid  user_role ---> uid rid=3
        let user = await this.app.mysql.query('insert into userlist (username,password,num) values (?,?,?)',[username,password,num])
        console.log(user);
        let uid = user.insertId;
        let rid = 3;//学生
        await this.app.mysql.query('insert into user_role (uid,rid) values (?,?)',[uid,rid])
    }
    //查询
    async selectUser(num){
        return await this.app.mysql.query('select * from userlist where num=?',[num])
    }
    //登录
    async login(num,password){
        return this.app.mysql.query('select * from userlist where num=? and password=?',[num,password])
    }
    //查询对应rid
    async getRid(uid){
        return this.app.mysql.query('select rid from user_role where uid=?',[uid])
    }

    async getRolename(rid){
        return this.app.mysql.query('select role_name from rolelist where id=?',[rid])
    }

    async notAdd(){
        let time = moment(new Date()).format('YYYY-MM-DD'); //2020-03-11
        //1.今日谁录入成绩
        return await this.app.mysql.query('select * from userlist where num not in (select num from scorelist where DATE_FORMAT(create_time,"%Y-%m-%d")=?)',[time])
    }
}

module.exports = UserService;
